"""
    执行入口
"""
import os
import sys
import requests

# 获取命令行参数
args = sys.argv
# 测试用例路径
test_path = args[1]
# 在线报告保存的路径
report_save_path = args[2]


def send_ding_msg(url, title, msg, msg_url):
    """
    发送钉钉通知
    :param url:
    :param title:
    :param msg:
    :param msg_url:
    :return:
    """
    ding_msg = {
        "msgtype": "link",
        "link": {
            "text": msg,
            "title": title,
            "picUrl": "http://localhost:8000/logo.png",
            "messageUrl": msg_url
        }
    }
    # 发送钉钉通知
    requests.post(url, json=ding_msg)


if __name__ == '__main__':
    # 清除reports下所有报告文件
    os.system("rm -rf reports")
    os.system("mkdir reports")
    os.system(f"/usr/local/bin/hrun {test_path}  --log-level DEBUG")
    # 获取最新的报名名
    latest_report = os.listdir("./reports")[-1]
    # 复制报告到tomcat挂载路径下
    if report_save_path:
        os.system(f"cp ./reports/{latest_report} {report_save_path}")
    # 测试完毕发送钉钉通知
    send_ding_msg("https://oapi.dingtalk.com/robot/send?access_token=bb23ce50dbd8e81cbebcb679b2be8"
                  "ca4b209218a2a47ba95fcc70f60fc14ca28", "自动化监控ci/cd提醒",
                  "自动化监控ci/cd提醒：您的自动化已执行完毕，点击查看报告！", f"http://localhost:8000/{latest_report}")
